using System;
using Dimok.Data.DataSources;

namespace Data.DataSources {
	public class PurchaseContractsDataSource : LoanBaseDataSource {
		private string _svn = "v_PurchaseContract";
		public override string SelectViewName { get { return _svn; } set{_svn = value;} }
		public override string InsertViewName { get { return "t_Loan_ContractConditions"; } }

		public int ContractHolderID { set { SetSelectParameter(CONTRACT_HOLDER_ID, value); } }
		public void ContractHolderIDRemove() { RemSelectParameter(CONTRACT_HOLDER_ID); }
		public int StatusID { set { SetSelectParameter("StatusID", value); } }
		public void StatusIDRemove() { RemSelectParameter(Status_ID); }
		public int BatchID { set { SetSelectParameter("BatchID", value); } }
		public void BatchIDRemove() { RemSelectParameter("BatchID"); }

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);
			Selecting += new System.Web.UI.WebControls.ObjectDataSourceSelectingEventHandler(PurchaseContractsDataSource_Selecting);
		}

		void PurchaseContractsDataSource_Selecting(object sender, System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs e) {
			if (GetSelectParameter(CONTRACT_HOLDER_ID) != null && GetSelectParameter(Status_ID) != null)
				SelectMethod = "SelectByStatusAndContractHolder";
			else if (GetSelectParameter(Status_ID) != null)
				SelectMethod = "SelectByStatus";
			else if (GetSelectParameter(CONTRACT_HOLDER_ID) != null)
				SelectMethod = "SelectByContractHolder";
			else if (GetSelectParameter("BatchID") != null)
				SelectMethod = "SelectByBatch";
			else SelectMethod = SELECT_METHOD;
		}

		public override System.Data.DataView SelectEx() {
			return base.SelectEx();
		}
		public System.Data.DataView SelectByStatus(int StatusID) {
			return Select("StatusID=@StatusID", StatusID);
		}

		#region SelectByStatusAndContractHolder
		public System.Data.DataView SelectByContractHolder(int ContractHolderID) {
			return Select("ContractHolderID=@CHID", ContractHolderID);
		}
		public System.Data.DataView SelectByStatusAndContractHolder(int StatusID, int ContractHolderID) {
			if (ContractHolderID < 0) return Select("StatusID=@StatusID", StatusID);
			return Select("StatusID=@StatusID AND ContractHolderID=@CHID", StatusID, ContractHolderID);
		}
		#endregion

		#region SelectByBatch
		public System.Data.DataView SelectByBatch(int BatchID) {
			UseNoLock = false;
			SelectViewName = "PurchaseBatch_Loans(@BatchID)";
			return Select("", BatchID);
		}
		#endregion

	}
}